package com.www.applyword.ui.chat;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.www.applyword.Constants.Constants;
import com.www.applyword.R;
import com.www.applyword.adapter.DynamicAdapter;
import com.www.applyword.base.BaseActivity;
import com.www.applyword.bean.DynaminBean;
import com.www.applyword.http.CallBack;
import com.www.applyword.http.HttpClient;
import com.www.applyword.utils.ImageUtil;
import com.www.applyword.utils.SPUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * author : huang
 * date   : 2019/11/11 0011 18:00
 * version: 1.0
 *
 * @author Administrator
 */
public class MyDynaminActivity extends BaseActivity {
    @BindView(R.id.rv_dynamic)
    RecyclerView rvDynamic;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refresh;
    @BindView(R.id.header)
    ClassicsHeader header;
    @BindView(R.id.civ_personal_head_image)
    CircleImageView civPersonalHeadImage;
    @BindView(R.id.tv_name)
    TextView tvName;
    private DynamicAdapter mDynamicAdapter;

    private int page = 1;
    private int Rows = 10;
    private List<DynaminBean.ListBean> list;
    ViewStub vsEmpty;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.my_dynamin_activity);
        setHead_title(View.GONE);
        ImageUtil.setImage(civPersonalHeadImage,SPUtil.getString(Constants.PHOTO));
        tvName.setText(SPUtil.getString(Constants.NAME));
    }

    @Override
    protected void loadData() {
        rvDynamic.setLayoutManager(new LinearLayoutManager(this));
        rvDynamic.setItemAnimator(new DefaultItemAnimator());
        mDynamicAdapter = new DynamicAdapter(this);
        rvDynamic.setAdapter(mDynamicAdapter);

        refresh.setOnRefreshListener(refreshLayout -> getForumList(true));

        refresh.setOnLoadMoreListener(refreshLayout -> getForumList(false));

        getForumList(true);

    }

    private void getForumList(boolean isRefresh) {
        if (isRefresh) page = 1;
        Map<String, Object> params = new HashMap<>();
        params.put("Page", page + "");
        params.put("Rows", Rows + "");
   //     params.put("Address", SPUtil.getString("city"));
        params.put("UserID", SPUtil.getString(Constants.USERID));
        params.put("ViewInformationID", SPUtil.getString(Constants.USERID));
        HttpClient.get(this, Constants.GETFORUMLIST, params, new CallBack<DynaminBean>() {
            @Override
            public void onSuccess(DynaminBean result) {
                list = result.getList();
                if (isRefresh) {
                    if (result.getList().size() == 0) {
                        showEmpty(true, vsEmpty, 2);
                    } else {
                        showEmpty(false, vsEmpty, 2);
                    }
                    refresh.finishRefresh(true);
                    mDynamicAdapter.replaceAll(result.getList(), true);
                } else {
                    if (result.getList().size() > 0) {
                        mDynamicAdapter.addAll(result.getList());
                        refresh.finishLoadMore(true);
                    } else {
                        refresh.finishLoadMoreWithNoMoreData();
                    }
                }
                page++;
            }
        });

        mDynamicAdapter.setOnItemClickListener((viewHolder, view, i) ->
                startActivity(new Intent(this, DynamicDetailsActivity.class)
                        .putExtra("ForumID", mDynamicAdapter.getItem(i).getForum_ID())));

    }


    @OnClick(R.id.ib_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
